package com.zthz.douxing.taskinfo

import com.zthz.douxing.TaskInfo
import com.zthz.douxing.sendmessageservice.service.AbstractHandlerService
import groovy.time.TimeCategory
import redis.clients.jedis.Jedis

class SearchTaskInfoJob {

    def grailsApplication
    def redisService

    def execute() {
        // execute job
       /*
        Date now = new Date()
        Date before2Minutes
        Date later1Minutes

        use(TimeCategory) {
            before2Minutes = now-5.minutes
            later1Minutes = now+5.minutes
        }List<TaskInfo> taskInfos = TaskInfo.findAllByStatusAndDateBeginExeBetween(0,before2Minutes,later1Minutes)
        List<TaskInfo> taskInfos = TaskInfo.findAllByStatus(0)

        if(taskInfos&&taskInfos?.size()>0){
            taskInfos.each {TaskInfo taskInfo ->
                executeTaskInfo(taskInfo)
            }
        }*/
    }

    protected excuteJobByName(String keyRegExp){
        Set<String> tasks = keys(keyRegExp)

            if(tasks && tasks.size()>0){
            tasks.each {String keyName ->
                if(isTakeWithTime(keyName)){
                    def taskId = redisService.getTaskFromQueue(keyName)
                    if(taskId){
                        TaskInfo task = TaskInfo.get(Long.parseLong(taskId))
                        try{
                            if(task&&task.runedCount<=task.maxCount)//反复执行最大次数限制内才能执行
                                executeTaskInfo(task)
                            task.runedCount++//每次都加1
                            task.save()
                        }catch (Exception e){
                            e.printStackTrace()
                            redisService.sendTaskToQueue(keyName,taskId)
                        }

                    }
                }
            }
        }
    }

    protected Set<String> keys(String keyPrix){
        return redisService.withRedisHasParameter ({Jedis jedis,String keyName ->
            return jedis.keys(keyName)
        },keyPrix)
    }
    protected void executeTaskInfo(TaskInfo taskInfo){
        String service = taskInfo.serviceType
        AbstractHandlerService handlerService = grailsApplication.getMainContext().getBean(service)
        handlerService.excute(taskInfo)
    }
    private isTakeWithTime(String keyName){
        String keyPattern =keyName.substring(keyName.lastIndexOf("-")+1)
        def result = false
        try{
            Date before2Minute
            Date ater2Minute
            Date now = new Date()
            Long executeTime = Long.parseLong(keyPattern)
            use(TimeCategory){
                before2Minute = now - 2.minutes
                ater2Minute = now+2.minutes
            }
            if(before2Minute.getTime()<=executeTime && ater2Minute.getTime()>=executeTime)
                result = true
            if(executeTime<=before2Minute.getTime())
                result = true
        } catch(Exception e){
            e.printStackTrace()
            throw e
        }

        return result
    }
}
